#ifndef STATEIDLE_H
#define STATEIDLE_H

#include "state.h"
#include "stateid.h"
#include "../manager.h"
#include "../eventmanager.h"
#include "../route.h"
#include "../properties.h"

namespace state {

struct IdleWait : public State {
	IdleWait(int id) : State(id) {}
	
	virtual void onEnter() {
		PropAccessor p;
		p().air_data.reset_alt = true;
		p().air_data.reset_spd = true;
		p().fcs.state = getId();
	}
};


const uint32_t CHECK_TIME = 500;

struct IdleCheck1 : public State {
	IdleCheck1(int id) : State(id) {}
	
	virtual void onEnter() {
		Manager::Instance().getEventManager()->setTimer(CHECK_TIME);

		PropAccessor p;
		p().fcs.roll.set(Roll_Direct, -1);
		p().fcs.pitch.set(Pitch_Direct, -1);
		p().fcs.state = getId();
	}
};

struct IdleCheck2 : public State {
	IdleCheck2(int id) : State(id) {}
	
	virtual void onEnter() {
		Manager::Instance().getEventManager()->setTimer(CHECK_TIME);

		PropAccessor p;
		p().fcs.roll.set(Roll_Direct, 1);
		p().fcs.pitch.set(Pitch_Direct, 1);
		p().fcs.state = getId();
	}
};


struct IdleReady : public State {
	IdleReady(int id) : State(id) {}
	
	virtual void onEnter() {
		PropAccessor p;
		p().fcs.roll.set(Roll_Neutral);
		p().fcs.pitch.set(Pitch_Neutral);
		p().fcs.state = getId();
	}
};


struct Idle : public State{
	Idle() : State() {
		State *wait = new IdleWait(stateid::IDLE_WAIT);
		State *check1 = new IdleCheck1(stateid::IDLE_WAIT);
		State *check2 = new IdleCheck2(stateid::IDLE_WAIT);
		State *ready = new IdleReady(stateid::IDLE_READY);

		setInitial(wait);

		wait->addTransition(evt_ready, check1);
		check1->addTransition(evt_timer, check2);
		check2->addTransition(evt_timer, ready);
		ready->addTransition(evt_failure, wait);
		ready->addTransition(evt_cmd_auto, NULL);
		ready->addTransition(evt_cmd_engine_start, NULL);
	}

	virtual void onEnter() {
		Manager::Instance().getRoute()->flyTo(0);

		PropAccessor p;
		p().fcs.roll.set(Roll_Neutral);
		p().fcs.pitch.set(Pitch_Neutral);
		p().fcs.yaw.set(Yaw_Neutral);
		p().fcs.throttle.set(Throttle_Value, -1);
		p().fcs.flaps.set(Flaps_Value, Flaps_Value_Retracted);
		p().fcs.brakes.set(Brakes_Value, 0);
		p().fcs.watcher = Watcher_Idle;
		
		p().fcs.parachute_armed = false;		
	}
};

}

#endif
